package com.xxhui.circleiamgeviewdemo.View;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.xxhui.circleiamgeviewdemo.R;

/**
 * Created by xxhui on 2016/8/6.
 * 这个是通过bitmapShader实现
 */
public class CircleImageViewByBitmapShader extends ImageView {

    private int width;
    private int height;
    private int radius;

    public CircleImageViewByBitmapShader(Context context) {
        super(context);
    }

    public CircleImageViewByBitmapShader(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
        radius = Math.min(width, height) / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.GREEN);//设置画布背景方便观察
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.photo);//夹在要画的图片，这里从资源文件，从其他地方也是可以的，比如sd卡
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(bitmapShader);//给画笔设置渲染器，这里画笔画过的面积就是渲染器里面的图片
        canvas.drawCircle(width / 2, height / 2, radius, paint);//这里画一个实心圆，就是将实心圆大小的渲染器图片用画笔画在画布上，说的好抽象，自己加自己理解吧，这是我的理解
        //canvas.drawRect(0,0,width,height,paint);
    }
}
